var config = require('../../../config/config.js');
var model = require('../../../model/model.js')
var show = false;
var item = {};
var index = 0;
Page({
  data: {
    name: "",
    mobile: "",
    addreValue: 0,
    address: "",
    province_id: "110000",
    city_id: "110100",
    area_id: '',
    aid: 1,
    mid: 1,
    sid: 1,
    province: '北京市',
    city: '市辖区',
    county: '',
    area_info: '',
    item: {
      show: show
    },
  },

  formSubmit: function (e) {
    var warn = "";
    var that = this;
    var flag = false;
    if (e.detail.value.name == "") {
      warn = "请填写您的姓名！";
    } else if (e.detail.value.mobile == "") {
      warn = "请填写您的手机号！";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.mobile))) {
      warn = "手机号格式不正确";
    } else if (e.detail.value.address == "") {
      warn = "请输入您的具体地址";
    } else {
      flag = true;
    }
    if (flag == false) {
      wx.showToast({
        title: warn,
        image:'/image/home/fail(1).png'
      })
      return
    }
    //提交表单内容
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_add,
      method: 'POST',
      data: {
        name: e.detail.value.name,
        mobile: e.detail.value.mobile,
        address: e.detail.value.address,
        mid: member_info.mid,
        sid: member_info.sid,
        aid: member_info.aid,
        province_id: this.data.province_id,
        city_id: this.data.city_id,
        area_id: this.data.area_id,
        area_info: this.data.province + ' ' + this.data.city + ' ' + this.data.county,
      },
      success: function (res) {
      wx.showToast({
        title: res.data.msg,
       duration:1000,
      })
        if (res.data.status) {
          // var reback = 2
          // wx.navigateTo({
          //   url: '../address/address?reback=' + reback,
          // })
          wx.navigateBack({
            delta:1
          })
        }
      },
      fail:function(){

      }
    })
  },

  /**
 * 生命周期函数--监听页面加载
 */

  onLoad: function (options) {
    var member_info = (wx.getStorageSync('member_info'));
    this.setData({
      mid: member_info.mid
    })
    if (options.type) {
      this.setData({
        name: options.name,
        address: options.address,
        area_info: options.area_info,
        mobile: options.mobile,
        // toshow: options.toshow
      })
    }
  },

  //生命周期函数--监听页面初次渲染完成
  onReady: function (e) {
    var that = this;
    //请求数据
    model.updateAreaData(that, 0, e);
  },
  //点击选择城市按钮显示picker-view
  translate: function (e) {
    model.animationEvents(this, 0, true, 400);
  },
  //隐藏picker-view
  hiddenFloatView: function (e) {
    model.animationEvents(this, 200, false, 400);
  },
  //滑动事件
  bindChange: function (e) {
    model.updateAreaData(this, 1, e);
    item = this.data.item;
    this.setData({
      province: item.provinces[item.value[0]].name,
      city: item.citys[item.value[1]].name,
      county: item.countys[item.value[2]].name,
      province_id: item.provinces[item.value[0]].code,
      city_id: item.citys[item.value[1]].code,
      area_id: item.countys[item.value[2]].code,
    });
  },
  onReachBottom: function () {
  },
  nono: function () { },

})